package com.projectsexception.festivales.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
    
    public static final String DATABASE_NAME = "festivales.db";
    private static final int DATABASE_VERSION = 1;
    
    public static final String FEEDS_TABLE = "feeds";
    public static final String MAPS_TABLE = "maps";
    public static final String SHOWS_TABLE = "shows";
    public static final String STAGES_TABLE = "stages";
    public static final String BANDS_TABLE = "bands";
    public static final String OFFICIAL_CHANNELS_TABLE = "officialChannels";
    public static final String FAVORITES_TABLE = "favorites";
    public static final String CACHE_TABLE = "cache";
    
    public static final String COL_ID = "id";
    public static final String COL_NAME = "name";
    public static final String COL_LINK = "link";
    public static final String COL_DATE = "date";
    public static final String COL_START_TIME = "startTime";
    public static final String COL_END_TIME = "endTime";
    public static final String COL_BAND_ID = "bandId";
    public static final String COL_STAGE_ID = "stageId";
    public static final String COL_BIOGRAPHY = "biography";
    public static final String COL_TYPE = "type";
    public static final String COL_SHOW_ID = "showId";
    public static final String COL_CODE = "code";
    public static final String COL_UPDATED = "updated";
    public static final String COL_DATA = "data";
    
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + FEEDS_TABLE + " ("
                    + COL_ID    + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_NAME  + " TEXT NOT NULL, " 
                    + COL_LINK  + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + MAPS_TABLE + " ("
                    + COL_ID    + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_NAME  + " TEXT NOT NULL, " 
                    + COL_LINK  + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + SHOWS_TABLE + " ("
        + COL_ID            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_DATE          + " TEXT NOT NULL, " 
        + COL_START_TIME    + " TEXT NOT NULL, " 
        + COL_END_TIME      + " TEXT NOT NULL, " 
        + COL_BAND_ID       + " INTEGER NOT NULL, " 
        + COL_STAGE_ID      + " INTEGER NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + STAGES_TABLE + " ("
        + COL_ID            + " INTEGER PRIMARY KEY, "
        + COL_NAME          + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + BANDS_TABLE + " ("
        + COL_ID            + " INTEGER PRIMARY KEY, "
        + COL_NAME          + " TEXT NOT NULL, " 
        + COL_LINK          + " TEXT NOT NULL, " 
        + COL_BIOGRAPHY     + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + OFFICIAL_CHANNELS_TABLE + " ("
                    + COL_ID        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_BAND_ID   + " INTEGER NOT NULL, "  
                    + COL_TYPE      + " TEXT NOT NULL, "
                    + COL_LINK      + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + FAVORITES_TABLE + " ("
        + COL_ID        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_SHOW_ID   + " INTEGER NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + CACHE_TABLE + " ("
        + COL_ID        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_TYPE      + " TEXT NOT NULL, "
        + COL_CODE      + " TEXT NOT NULL, "
        + COL_UPDATED   + " INTEGER NOT NULL, " 
        + COL_DATA      + " BLOB NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }

}
